<?php
/**
 * Registry
 * @filesource
 */
/**
 * Namespace
 */
namespace framework\library;
/**
 * Dependencies
 */
use framework\library\Loader;
use framework\library\interfaces\Registry_Interface;
/**
 * Registry Class
 *
 * Plurious Core
 *
 * Simple registry 
 * 
 * Stores/retrieves key/value pairs.  Values can be arrays and array elements
 * can be retrieved.
 * 
 * @author Randy Casburn <randy@plurious.com>
 * @copyright Copyright Plurious Software, all rights reserved
 * @license MIT
 * @license http://www.opensource.org/licenses/mit-license.html
 * @link http://plurious.com/framework/documentation/classes/Front.html
 * @version 2.1
 * 
 * @uses framework\library\Loader
 */
class Registry extends Loader implements Registry_Interface {
    /** The registry store */
    private $data = array();
    /**
     * Return a value that corresponds to the given key
     * 
     * @param string $key the value corresponding to this key will be returned
     * @return mixed value corresponding the provided key
     */
    public function get($key) {
        return (isset($this->data[strtolower($key)]) ? $this->data[strtolower($key)] : NULL);
    }
    /**
     * Store the key/value pair in the registry
     * 
     * @param string $key
     * @param mixed $value 
     */
    public function set($key, $value) {
        $this->data[strtolower($key)] = $value;
    }
    /**
     * Check for a value in the registry that corresponds to the given key
     * 
     * @param string $key to search for
     * @return boolean True if value exists for the given key
     */
    public function has($key) {
        return isset($this->data[strtolower($key)]);
    }
    /**
     * Appends an element to a registered array
     * 
     * @param string $array_name Name of registry key containing array to append data to
     * @param mixed $data the data to append to $array_name
     * @param string $keyname The key to associate with the appended value
     */
    public function setElement($array_name, $data, $keyname = '') {
        if(!$keyname){
            $this->data[$array_name][] = $data;
        } else {
            $this->data[$array_name][$keyname] = $data;
        }
    }
    /**
     *
     * Retrieves a single element (which may contain an array) from a registered array
     * 
     * @param array $array The array to retrieve the element (key) from
     * @param string $key The key of the associated array element
     * @return mixed The data associated with the key or null if $array is not an array
     */
    public function getElement($array, $key) {
        $base = $this->get($array);
        if (is_array($base)) {
            return $this->data[$array][$key];
        }
        return null;
    }

}

?>